<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 头部-->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="vodka" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDiaLog('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形结构的部门的数据 -->
        <el-tree :data="list" :default-expand-all="true" :props="{ children: 'children', label: 'manager' }">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--#default === slot-scope 什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)-->
          <template #default="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDiaLog(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length===0"
                          @click.native="hDel(data.id)"
                        >
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :visible.sync="showDialog"
        :title="isEdit?'编辑':'新增'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hdialogClose"
      >
        <!-- <AddorEdit v-if="showDialog" :id="curId" :is-edit="isEdit" @success="closeDialog" /> -->
        <AddorEdit
          :id="curId"
          ref="depDialog"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="closeDialog"
          @close="hClose"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddorEdit from './deptDialog.vue'
export default {
  name: 'Departments',
  components: {
    AddorEdit
  },
  data() {
    return {
      // 获取部门列表的数据
      list: [],
      // 控制弹层的显示或者隐藏
      showDialog: false,
      // 当前部门的id值
      curId: '',
      isEdit: false, // false代表新增  true代表编辑
      // 校验数据
      originList: []
    }
  },
  created() {
    // 进入页面就加载部门数据
    this.LoaDepartments()
  },
  methods: {
    // 关闭清空表单
    hdialogClose() {
      this.$refs.depDialog.resetForm()
    },
    // 获取部门数据
    async LoaDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res.data.data.depts)
        this.originList = res.data.data.depts
          .map(({ code, id, name, pid }) => {
            return { code, id, name, pid }
          })
        // 删除数组中第一条数据 公司的名称
        res.data.data.depts.shift()
        this.list = tranListToTreeData(res.data.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 弹出新增dialog框
    addDiaLog(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 关闭dialog框
    closeDialog() {
      this.showDialog = false
      this.LoaDepartments()
    },
    // 编辑部门
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      // 引入子组件中的loadDepartmentDetail函数调用，刷新表单
      setTimeout(() => {
        this.$refs.depDialog.loadDepartmentDetail()
      }, 0)
    },
    // 删除部门
    hDel(id) {
      this.$confirm('是否删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          await delDepartment(id)
          // console.log(res, '删除')
          // 刷新页面
          this.LoaDepartments()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 关闭弹框
    hClose() {
      this.showDialog = false
    }
  }
}
</script>

<style scope>

</style>
